//
//  ToolDB.m
//  BeautifulShoppings
//
//  Created by GivenBmli on 16/8/31.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "ToolDB.h"
#import <sqlite3.h>
#import "GuideMiddleModel.h"
#import "ToolModel.h"

@interface ToolDB ()

@end

static sqlite3 *db = nil;

@implementation ToolDB
// 打开数据库并创建表


+ (void)openDataBase{
    
    if (db != nil) {
        return;
    }
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    path = [path stringByAppendingPathComponent:@"gift.sqlite"];
    //NSLog(@"%@",path);
    int result = sqlite3_open(path.UTF8String, &db);
    if (result == SQLITE_OK) {
        // NSLog(@"数据库打开成功");
    }else{
        // NSLog(@"数据库打开失败,%d",result);
    }
    
}

+(void)closeDataBase{
    int result = sqlite3_close(db);
    db = nil;
    if (result == SQLITE_OK) {
        //  NSLog(@"数据库关闭成功");
    }else{
        //  NSLog(@"数据库关闭失败,%d",result);
    }
    
    
}



+(void)createTable{
    [self openDataBase];
    
    NSString *sql  = @"CREATE TABLE IF NOT EXISTS t_activey(Id integer PRIMARY KEY autoincrement, title TEXT,cover_image_url TEXT);";
    
    // NSString *sql  = @"CREATE TABLE IF NOT EXISTS t_activey(myid integer PRIMARY KEY autoincrement, title TEXT,cover_image_url TEXT, id text);";
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        // NSLog(@"表格创建成功");
    }else{
        // NSLog(@"表格创建失败,%d",result);
    }
    [self closeDataBase];
    
}




+(void)createTableS{
    [self openDataBase];
    
    NSString *sql = @"CREATE TABLE IF NOT EXISTS t_ACE(Id integer PRIMARY KEY, title TEXT,cover_image_url TEXT);";
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        //  NSLog(@"表格创建成功");
    }else{
        //   NSLog(@"表格创建失败,%d",result);
    }
    [self closeDataBase];
    
}

+(void)createTableTwo{
    [self openDataBase];
    
    NSString *sql = @"CREATE TABLE IF NOT EXISTS t_Hot(Id integer PRIMARY KEY, title TEXT,cover_image_url TEXT);";
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        //  NSLog(@"表格创建成功");
    }else{
        //   NSLog(@"表格创建失败,%d",result);
    }
    [self closeDataBase];
    
}




+ (void)execSqlString:(NSString *)str
{
    [self openDataBase];
    char *errmsg = NULL;
    
    sqlite3_exec(db, str.UTF8String, NULL, NULL, &errmsg);
    
    if (errmsg) {
        // NSLog(@"错误为");
    } else {
        //NSLog(@"插入成功");
    }
    [self closeDataBase];
}


// 查询
+ (NSMutableArray *)selectFromSql:(NSString *)sql
{
    [self openDataBase];
    sqlite3_stmt *stmt = NULL;
    
    int result = sqlite3_prepare(db, sql.UTF8String, -1, &stmt, 0);
    
    NSMutableArray *arr = [NSMutableArray array];
    
    if (result == SQLITE_OK) {
        
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const char *name = (const char *)sqlite3_column_text(stmt, 0);
            const char *cover_image_url =(const char *)sqlite3_column_text(stmt,1);
            //NSInteger Id =sqlite3_column_int64(stmt, 2);
            NSInteger Id = sqlite3_column_double(stmt, 2);
            
            NSString *str = [NSString stringWithUTF8String:name];
            NSString *str2 = [NSString stringWithUTF8String:cover_image_url];
            
            ToolModel *model = [[ToolModel alloc]init];
            model.title = str;
            model.cover_image_url = str2;
            model.Id = [NSString stringWithFormat:@"%ld", (long)Id];
            
            [arr addObject:model];
        }
    }
    sqlite3_finalize(stmt);
    [self closeDataBase];
    return arr;
}

+ (void)dele:(NSString *)sql
{
    [self openDataBase];
    
    char *errmsg = nil;
    //   int result = sqlite3_exec(db, sql.UTF8String, nil, nil, &error);
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, &errmsg);
    if (result == SQLITE_OK) {
        // NSLog(@"删除成功");
    }
    else
    {
        //NSLog(@"删除失败%d",result);
    }
    [self closeDataBase];
}

@end
